package com.meihua.shellapp.utils;

import android.widget.ImageView;

import com.meihua.shellapp.R;
import com.meihua.shellapp.base.MyApplication;
import com.squareup.picasso.Picasso;

import java.io.File;

/**
 * 作者：lc on 2017/10/11.
 * 邮箱：lc0607live@163.com
 */

public class PicassoUtils {

    private PicassoUtils() {
    }

    private static PicassoUtils utils;

    public static PicassoUtils getInstance() {
        if (utils == null) {
            utils = new PicassoUtils();
        }
        return utils;
    }

    /**
     * 绑定图片（原图）
     *
     * @param imageView
     * @param url
     */
    public void bindImageData(ImageView imageView, String url) {
        if (url == null)
            return;
        if (url.indexOf("http") == -1) {
            url = URL.getUrl().HOST + url;
        }
        Picasso.with(MyApplication.getInstance().getApplicationContext())
                .load(url)
                .fit()
                .placeholder(R.drawable.ic_default)
                .into(imageView);
    }

    /**
     * 绑定图片（原图）
     *
     * @param imageView
     * @param url
     */
    public void bindImageData(ImageView imageView, String url, int defaultimg) {
        if (url == null)
            return;
        if (url.indexOf("http") == -1) {
            url = URL.getUrl().HOST + url;
        }
        Picasso.with(MyApplication.getInstance().getApplicationContext())
                .load(url)
                .placeholder(defaultimg)
                .into(imageView);
    }

    /**
     * 绑定图片(本地)
     *
     * @param imageView
     * @param url
     */
    public void bindImageDataAllPath(ImageView imageView, File url) {
        Picasso.with(MyApplication.getInstance().getApplicationContext())
                .load(url)
                .placeholder(R.drawable.ic_default)
                .into(imageView);
    }

    /**
     * 绑定图片（缩略图）
     *
     * @param imageView
     * @param url
     */
    public void bindImageDataThumbnail(ImageView imageView, String url) {
        if (url == null)
            return;
        if (url.indexOf("http") == -1) {
            url = URL.getUrl().HOST + url;
        }
        Picasso.with(MyApplication.getInstance().getApplicationContext())
                .load(url)
//                .resize(dp2px(250), dp2px(250))
                .centerCrop()
                .into(imageView);
    }
}
